/* Developed by Vinh, Nguyen Hong <mansofta@gmail.com>
 * Copyright HIENDAI SOFTWARE COMPANY 2004
 * http://www.hiendai.com.vn
 * contact@hiendai.com.vn
*/
using System;
using System.Xml;

namespace MarcXmlParser {

	public class CSubfield
	{
	
	protected	XmlDocument ptCurrentDoc;
	protected	XmlNode ptCurrentRootNode;		
	

	public CSubfield()
		{		
			ptCurrentDoc=new XmlDocument();				
			ptCurrentDoc.LoadXml("<subfield code='a'></subfield>"); 
			ptCurrentRootNode=ptCurrentDoc.SelectSingleNode("./*[local-name() ='subfield']"); 		
		}
	public void ReConstruct()
		{
			ptCurrentDoc.LoadXml("<subfield code='a'></subfield>"); 
			ptCurrentRootNode=ptCurrentDoc.SelectSingleNode("./*[local-name() ='subfield']"); 				
		}
	public bool load_Xml(String SubfieldStream) //only available if pXMLDOM is NULL
		{
			ptCurrentDoc.LoadXml(SubfieldStream);
			ptCurrentRootNode=ptCurrentDoc.SelectSingleNode("./*[local-name() ='subfield']");
			if (ptCurrentRootNode!=null)
				return true;
			else
				return false;
		}
	public bool load_RootNode(ref XmlNode ptSubfieldRootNode)
		{
			ptCurrentRootNode=ptSubfieldRootNode;
			if (ptCurrentRootNode.Name=="subfield")
			{
				ptCurrentDoc=ptCurrentRootNode.OwnerDocument;
				return true;
			}
			else
			{
				return false;
			}
		}
	public String Code
		{
			set
			{
				XmlElement xElem;//Data field node			
				xElem=(XmlElement)(ptCurrentRootNode); 
				xElem.SetAttribute("code", value);					
			}
			get
			{
				XmlNode mxNode = (XmlNode)(ptCurrentRootNode.Attributes.GetNamedItem("code"));
				if(mxNode!=null)
					return ((XmlAttribute)(mxNode)).Value;
				else
					return "";
			}
		}
	public String Value
		{
			set
			{
				XmlNode ptEditNode;
				if (ptCurrentRootNode.HasChildNodes) //child text node have been existed
				{
					ptEditNode=ptCurrentRootNode.FirstChild;
                    if ((isCDATA(value) && ptEditNode.NodeType == XmlNodeType.CDATA) || !isCDATA(value) && ptEditNode.NodeType != XmlNodeType.CDATA)
                    {
                        ptEditNode.Value = value;
                    }
                    else
                    {
                        ptCurrentRootNode.RemoveChild(ptEditNode);
                        if (isCDATA(value))
                        {
                            ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.CreateCDataSection(value));
                        }
                        else
                        {
                            ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.CreateTextNode(value));
                        }
                    }
					
				} else // Create new text node then add to the parent node
				{
                    if (isCDATA(value))
                    {
                        ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.CreateCDataSection(value));
                    }
                    else
                    {
                        ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.CreateTextNode(value));
                    }
				}			
			}
			get
			{
				if(ptCurrentRootNode!=null)
					return ptCurrentRootNode.InnerText;
				else
					return "";			
			}		
		}
		public String OuterXml
		{
			get
			{
				return ptCurrentRootNode.OuterXml;
			}
		}
        public String InnerText
        {
            get
            {
                return ptCurrentRootNode.InnerText;
            }
        }
		public XmlNode RootNode
		{
			set
			{
				ptCurrentRootNode=value;
				ptCurrentDoc=ptCurrentRootNode.OwnerDocument;
			}			
			get
			{
				return ptCurrentRootNode;
			}
			
		}
        private bool isCDATA(String sData)
        { 
            bool ret=false;
            if ((sData.IndexOf('>') >= 0) || (sData.IndexOf('<') >= 0))
            {
                ret = true;
            }
            return ret;
        }
	};


}
